<?php

namespace App\Jobs;

use App\Http\Service\CountryService;
use App\Model\Device;
use App\Model\DeviceWarning;
use App\Model\House;
use App\User;
use App\Utils\Result;
use Faker\Core\DateTime;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Storage;


class CheckWarning implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    private $deviceId;
    public $tries = 2;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($deviceId)
    {
        $this->deviceId = $deviceId;
        //因为要导入大文件 需要很久时间
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        try{
            $today = new \DateTime();
            if(Redis::exists('LOW_DAY_WARNING')){
                $day = Redis::get('LOW_DAY_WARNING');
            }else{
                $day = 7;
            }
            $AllXh = 0;
            $start = null;
            $end = null;
            if($day > 0){
                for($i=1; $i<=$day; $i++){
                    $Ago = clone $today; // 克隆当前日期对象，避免修改原始对象
                    $Ago->modify("-{$i} day");
                    $dateKey = $this->deviceId.'-'.$Ago->format('Ymd');
                    if($i == 1){
                        $end =  $Ago->format('Y-m-d');
                    }
                    if($i == $day){
                        $start =  $Ago->format('Y-m-d');
                    }
                    if(Redis::exists($dateKey)){
                        $xh  = Redis::get($dateKey);
                    }else{
                        $xh = 0;
                    }
                    if($this->deviceId == 722){
                        Log::info($dateKey.'-----'.$xh);
                    }
                    $AllXh += $xh;
                }
                if(Redis::exists('LOW_DAY_WARNING')){
                    $power = Redis::get('LOW_POWER_WARNING');
                    if($AllXh < $power){
                        $device = Device::query()->where('id',$this->deviceId)->first();
                        $house = House::query()->where('id',$device->house_id)->first();
                        if($device->bind_at){
                            $bindTime = strtotime($device->bind_at);
                            if($bindTime < strtotime($start. '00:00:00')){
                                $device->is_warning = 1;
                                $device->save();
                                $save['xh'] = $AllXh;
                                $save['start'] = $start;
                                $save['end'] = $end;
                                $save['sn']  = $device->sn;
                                $save['device_id'] = $this->deviceId;
                                $save['house_id'] = $device->house_id;
                                $save['address'] = $house->province.$house->city.$house->district.$house->country.$house->estate_name;
                                $save['lat'] = $house->lat;
                                $save['lng'] = $house->lng;
                                $save['room'] = $house->name??'未知';
                                $save['tenant_mobile'] = $device->mobile;
                                $count = DeviceWarning::query()->where('device_id',$this->deviceId)
                                    ->where('end','<',$start)
                                    ->count();
                                if(!$count){
                                   $model =  DeviceWarning::create($save);
//                                    if($house->country_code && $house->is_push == 2){
//                                        $push['regionCode'] = $house->country_code;
//                                        $push['sn'] = $device->sn;
//                                        $push['warningDate'] = date('Y-m-d');
//                                        $push['startDate'] = $start;
//                                        $push['endDate'] = $end;
//                                        $push['consume'] =  $AllXh>0?($AllXh/1000):0;
//                                        $push['phone'] = $device->mobile;
//                                        $user = User::query()->where('mobile',$device->mobile)->first();
//                                        $push['name'] = $user->nickname?$user->nickname:'未实名';
//                                        $push['address'] = $save['address'];
////                                        $push['id'] = $save['id'];
//                                        $push['room'] = $house->name??'未知';
//                                        $countryService = new CountryService();
//                                        $countryService->pushWarning($push);
//                                    }
                                }
                            }
                        }else{
                            if($house && $house->created_at){
                                $bindTime = strtotime($house->created_at);
                                if($bindTime < strtotime($start. '00:00:00')){
                                    $device->is_warning = 1;
                                    $device->save();
                                    $save['xh'] = $AllXh;
                                    $save['start'] = $start;
                                    $save['end'] = $end;
                                    $save['sn']  = $device->sn;
                                    $save['device_id'] = $this->deviceId;
                                    $save['house_id'] = $device->house_id;
                                    $save['address'] = $house->province.$house->city.$house->district.$house->country.$house->estate_name;
                                    $save['lat'] = $house->lat;
                                    $save['lng'] = $house->lng;
                                    $save['tenant_mobile'] = $device->mobile;
                                    $count = DeviceWarning::query()->where('device_id',$this->deviceId)
                                        ->where('end','<',$start)
                                        ->count();
                                    if(!$count){
                                        $model = DeviceWarning::create($save);
//                                        if($house->country_code && $house->is_push == 2){
//                                            $push['regionCode'] = $house->country_code;
//                                            $push['sn'] = $device->sn;
//                                            $push['warningDate'] = date('Y-m-d');
//                                            $push['startDate'] = $start;
//                                            $push['endDate'] = $end;
//                                            $push['consume'] =  $AllXh>0?($AllXh/1000):0;
//                                            $push['phone'] = $device->mobile;
//                                            $user = User::query()->where('mobile',$device->mobile)->first();
//                                            $push['name'] = $user->nickname?$user->nickname:'未实名';
//                                            $push['address'] = $save['address'];
//                                            $push['room'] = $house->name??'未知';
////                                            $push['id'] = $save['id'];
//                                            $countryService = new CountryService();
//                                            $countryService->pushWarning($push);
//                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }catch (\Exception $e){
            Log::info($e->getMessage());
        }
    }
}

